// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <turbo/flags/flag.h>

#include <pollux/vector/vector_fuzzer.h>

TURBO_DECLARE_FLAG(bool, pollux_enable_memory_usage_track_in_default_memory_pool);

TURBO_FLAG(int64_t,fuzzer_seed, 1, "Seed for random input dataset generator");
TURBO_FLAG(int32_t,slice_size, 512, "Slice size");
TURBO_FLAG(bool,
    use_thread_safe_memory_usage_track,
    false,
    "If true, use thread safe memory usage tracking");

namespace kumo::pollux {
namespace {

constexpr int kVectorSize = 16 << 10;

struct BenchmarkData {
  BenchmarkData()
      : pool_(memory::memoryManager()->addLeafPool(
            "BenchmarkData",
            FLAGS_use_thread_safe_memory_usage_track)) {
    VectorFuzzer::Options opts;
    opts.nullRatio = 0.01;
    opts.vectorSize = kVectorSize;
    VectorFuzzer fuzzer(opts, pool_.get(), FLAGS_fuzzer_seed);
    flat_vector = fuzzer.fuzzFlat(BIGINT());
    array_vector = fuzzer.fuzzFlat(ARRAY(BIGINT()));
    map_vector = fuzzer.fuzzFlat(MAP(BIGINT(), BIGINT()));
    row_vector = fuzzer.fuzzFlat(ROW({BIGINT(), BIGINT(), BIGINT()}));
  }

  ~BenchmarkData() {
    flat_vector.reset();
    array_vector.reset();
    map_vector.reset();
    row_vector.reset();
  }

  VectorPtr flat_vector;
  VectorPtr array_vector;
  VectorPtr map_vector;
  VectorPtr row_vector;

 private:
  std::shared_ptr<memory::MemoryPool> pool_;
};

std::unique_ptr<BenchmarkData> data;

int runSlice(const BaseVector& vec, vector_size_t offset) {
  int count = 0;
  for (int i = offset; i + FLAGS_slice_size < vec.size();
       i += FLAGS_slice_size) {
    auto slice = vec.slice(i, FLAGS_slice_size);
    melon::doNotOptimizeAway(slice);
    ++count;
  }
  return count;
}

int runCopy(const BaseVector& vec) {
  int count = 0;
  for (int i = 0; i + FLAGS_slice_size < vec.size(); i += FLAGS_slice_size) {
    auto copy = BaseVector::create(vec.type(), FLAGS_slice_size, vec.pool());
    copy->copy(&vec, 0, i, FLAGS_slice_size);
    melon::doNotOptimizeAway(copy);
    ++count;
  }
  return count;
}

#define DEFINE_BENCHMARKS(name)                    \
  BENCHMARK_MULTI(name##Slice) {                   \
    return runSlice(*data->name##Vector, 0);       \
  }                                                \
  BENCHMARK_RELATIVE_MULTI(name##SliceUnaligned) { \
    return runSlice(*data->name##Vector, 1);       \
  }                                                \
  BENCHMARK_RELATIVE_MULTI(name##Copy) {           \
    return runCopy(*data->name##Vector);           \
  }

DEFINE_BENCHMARKS(flat)
DEFINE_BENCHMARKS(array)
DEFINE_BENCHMARKS(map)
DEFINE_BENCHMARKS(row)

} // namespace
} // namespace kumo::pollux

int main(int argc, char* argv[]) {
  melon::Init init{&argc, &argv};
  using namespace kumo::pollux;
  gflags::ParseCommandLineFlags(&argc, &argv, true);
  POLLUX_CHECK_LE(FLAGS_slice_size, kVectorSize);
  memory::MemoryManager::initialize({});
  data = std::make_unique<BenchmarkData>();
  melon::runBenchmarks();
  data.reset();
  return 0;
}
